package com.why.config;

import com.mysql.cj.util.StringUtils;

import java.sql.Driver;
import java.sql.SQLException;
import java.util.Objects;

/**
 * 基本配置类
 *
 * @author wanghongyang24
 */
public class SdbcpBaseConfig {
    /**
     * 连接地址
     */
    private String jdbcUrl;
    /**
     * 用户名
     */
    private String userName;
    /**
     * 密码
     */
    private String password;
    /**
     * 连接大小
     */
    private Integer coreConnectionSize;

    private Integer maxConnectionSize;
    /**
     * 超过core的连接存活时间
     */
    private Integer connecttionSaveTime;


    public SdbcpBaseConfig(String jdbcUrl, String userName, String password, Integer coreConnectionSize, Integer maxConnectionSize, Integer connecttionSaveTime) {
        this.jdbcUrl = jdbcUrl;
        this.userName = userName;
        this.password = password;
        this.coreConnectionSize = coreConnectionSize;
        this.maxConnectionSize = maxConnectionSize;
        this.connecttionSaveTime = connecttionSaveTime;

    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public String getUserName() {
        return userName;
    }

    public String getPassword() {
        return password;
    }

    public Integer getCoreConnectionSize() {
        return coreConnectionSize;
    }

    public Integer getMaxConnectionSize() {
        return maxConnectionSize;
    }

    public Integer getConnecttionSaveTime() {
        return connecttionSaveTime;
    }

    public static SdbcpBuilder Builder() {
        return new SdbcpBuilder();
    }

    public static class SdbcpBuilder {

        /**
         * 连接地址
         */
        private String jdbcUrl;
        /**
         * 用户名
         */
        private String userName;
        /**
         * 密码
         */
        private String password;
        /**
         * 连接大小
         */
        private Integer coreConnectionSize;

        private Integer maxConnectionSize;
        /**
         * 超过core的连接存活时间
         */
        private Integer connectionSaveTime;

        public SdbcpBuilder setJdbcUrl(String jdbcUrl) {
            this.jdbcUrl = jdbcUrl;
            return this;
        }

        public SdbcpBuilder setUserName(String userName) {
            this.userName = userName;

            return this;
        }

        public SdbcpBuilder setPassword(String password) {
            this.password = password;
            return this;
        }

        public SdbcpBuilder setCoreConnectionSize(Integer coreConnectionSize) {
            this.coreConnectionSize = coreConnectionSize;
            return this;
        }

        public SdbcpBuilder setMaxConnectionSize(Integer maxConnectionSize) {
            this.maxConnectionSize = maxConnectionSize;
            return this;
        }

        public SdbcpBuilder setConnectionSaveTime(Integer connectionSaveTime) {
            this.connectionSaveTime = connectionSaveTime;
            return this;
        }


        public SdbcpBaseConfig build() {
            /**
             * 参数校验
             */
            if (StringUtils.isNullOrEmpty(this.jdbcUrl)) {
                throw new NullPointerException("jdbcUrl 不能为空!");
            }
            if (StringUtils.isNullOrEmpty(password) || StringUtils.isNullOrEmpty(userName)) {
                throw new NullPointerException("用户名or密码不能为空!");
            }
            if (coreConnectionSize < 0 || coreConnectionSize > Integer.MAX_VALUE - 1) {
                throw new IllegalArgumentException("coreConnectionSiz小于0或者大于Integer.MAX_VALUE:" + coreConnectionSize);
            }
            if (maxConnectionSize < 0 || maxConnectionSize > Integer.MAX_VALUE - 1) {
                throw new IllegalArgumentException("maxConnectionSize小于0或者大于Integer.MAX_VALUE:" + maxConnectionSize);
            }
            return new SdbcpBaseConfig(jdbcUrl, userName, password, coreConnectionSize, maxConnectionSize, connectionSaveTime);
        }
    }


}
